﻿using GDI_应用案例.Pages.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GDI_应用案例.Pages
{
    [FormPage(3, "验证码")]
    public partial class PageVerificationCode : PageBase
    {
        readonly VerificationCode _verificationCode = new VerificationCode();
        public PageVerificationCode()
        {
            InitializeComponent();
            _verificationCode.TimeCallback += _verificationCode_TimeCallback;
        }

        private void _verificationCode_TimeCallback(long obj)
        {
            label7.Text = $"耗时：{obj}ms";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image?.Dispose();

            _verificationCode.CodeLength=(int)numericUpDown1.Value;
            _verificationCode.NoiseLine= (int)numericUpDown2.Value;
            _verificationCode.NoisePoint = (int)numericUpDown3.Value;
            _verificationCode.NormalCode = checkBox1.Checked;
            _verificationCode.IsDrawNoiseLine= checkBox2.Checked;
            _verificationCode.IsDrawNoisePoint= checkBox3.Checked;
            _verificationCode.Font = new Font(comboBox1.Text, (int)numericUpDown4.Value, _dic[comboBox2.Text]);

            var result = _verificationCode.CreateVerificationCode();
            pictureBox1.Image = result.Image;
            label8.Text=result.Code;
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null) return;
            FrmShowImage frmShow = new FrmShowImage(pictureBox1.Image);
            frmShow.ShowDialog();
            frmShow.Dispose();
        }

        private Dictionary<string, FontStyle> _dic= new Dictionary<string, FontStyle>();
        private void PageVerificationCode_Load(object sender, EventArgs e)
        {
            foreach (FontFamily font in FontFamily.Families)
            {
                comboBox1.Items.Add(font.Name);
            }
            comboBox1.SelectedIndex = 0;

            var array=Enum.GetValues(typeof(FontStyle));
            foreach (FontStyle font in array)
            {
                _dic.Add(font.ToString(), font);
                comboBox2.Items.Add(font.ToString());
            }
            comboBox2.SelectedIndex = 1;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}
